package com.taikang.hmp.hbusiness.doctor.information.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.properties.PropertiesHelper;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.hbusiness.diabetes.measure.medicine.service.impl.MedicineServiceImpl;
import com.taikang.hmp.hbusiness.doctor.information.bean.Advert;
import com.taikang.hmp.hbusiness.doctor.information.bean.Favorite;
import com.taikang.hmp.hbusiness.doctor.information.bean.Information;
import com.taikang.hmp.hbusiness.doctor.information.bean.Topinfo;
import com.taikang.hmp.hbusiness.doctor.information.service.MessageService;
import com.taikang.hmp.hbusiness.doctor.setbase.service.IUserBaseService;

/**
 * 类说明 :
 * 
 * @author :Andersonyali
 * @time : 2015-10-10 下午2:13:28
 */
public class MessageServiceImpl extends BaseServiceImpl implements
		MessageService {
	private static Logger logger = LoggerFactory.getLogger(MedicineServiceImpl.class);
	
	private IUserBaseService userBaseService;
	
	public IUserBaseService getUserBaseService() {
		return userBaseService;
	}

	public void setUserBaseService(IUserBaseService userBaseService) {
		this.userBaseService = userBaseService;
	}

	@Override
	public void saveBasicInfo(Dto dto) {
		Information inf = new Information();
		String information_id = java.util.UUID.randomUUID().toString();
		inf.setTitle(dto.getAsString("title"));
		// 图片的上传
		inf.setImage_url(dto.getAsString("image_url"));
		inf.setType(dto.getAsString("type"));
		inf.setReview_status("0");
		super.getNewDao().insert("message_space.insert_bs_message", inf);
	}
	
	@Override
	public Information getInformationById(String information_id) {
		super.getNewDao().update("message_space.updateReadcount", information_id);
		return (Information) super.getNewDao().queryForObject("message_space.getMessage_uid", information_id);
	}

	@Override
	public Dto getMessageList(Dto paramDto) {
		String doctorlogourl = PropertiesHelper.appHelper.getValue("doctorlogourl");
		Map<String,String> map = new HashMap<String,String>();
		if (paramDto.getAsString("app") == null||"".equals(paramDto.getAsString("app"))) {
			map.put("app", "doctor");
		} else {
			map.put("app", paramDto.getAsString("app"));
		}
		map.put("pageNo", paramDto.getAsString("pageNo"));
		map.put("type",paramDto.getAsString("type"));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		//添加系统当时时间，让提前发布的资讯不显示
		map.put("nowdate", sdf.format(new Date()));
		//List<Dto> result = new ArrayList<Dto>();
		Dto result = new BaseDto();
		if("最新".equals(paramDto.getAsString("type"))){
			List<Information> resultList = super.getNewDao().queryForList("message_space.getNewMessageList",map);
			Dto re = new BaseDto();
			int count = resultList.size();
			List<Dto> detailList = new ArrayList<Dto>();
			for (int i = 0; i < resultList.size(); i++) {
				Dto resultMap = new BaseDto();
				resultMap.put("informationid",resultList.get(i).getInformation_id());
				resultMap.put("title",resultList.get(i).getTitle());
				resultMap.put("type", resultList.get(i).getType());
				resultMap.put("summary", resultList.get(i).getSummary());
				if(resultList.get(i).getImage_url()==null){
					resultMap.put("picurl",doctorlogourl);
				}else{
					resultMap.put("picurl", resultList.get(i).getImage_url());
				}
				resultMap.put("content", resultList.get(i).getContent());
				resultMap.put("dates", resultList.get(i).getDates());
				//优化，增加阅读数和视频标识
				resultMap.put("readcount", resultList.get(i).getReadcount());
				//0无视频  1 有视频
				if(resultList.get(i).getVideoid()==null){
					resultMap.put("tag", "0");
				}else{
					resultMap.put("tag", "1");
				}
				detailList.add(resultMap);
			}
			result.put("detailList", detailList);
			result.put("count", count);
		}else{
			List<Information> resultList = super.getNewDao().queryForList("message_space.getMessageList_type",map);
			Dto re = new BaseDto();
			int count = resultList.size();
			List<Dto> detailList = new ArrayList<Dto>();
			for (int i = 0; i < resultList.size(); i++) {
				Dto resultMap = new BaseDto();
				resultMap.put("informationid",resultList.get(i).getInformation_id());
				resultMap.put("title",resultList.get(i).getTitle());
				resultMap.put("type", resultList.get(i).getType());
				resultMap.put("summary", resultList.get(i).getSummary());
				if(resultList.get(i).getImage_url()==null){
					resultMap.put("picurl",doctorlogourl);
				}else{
					resultMap.put("picurl", resultList.get(i).getImage_url());
				}
				resultMap.put("content", resultList.get(i).getContent());
				resultMap.put("dates", resultList.get(i).getDates());
				//优化，增加阅读数和视频标识
				resultMap.put("readcount", resultList.get(i).getReadcount());
				//0无视频  1 有视频
				if(resultList.get(i).getVideoid()==null){
					resultMap.put("tag", "0");
				}else{
					resultMap.put("tag", "1");
				}
				detailList.add(resultMap);
			}
			result.put("detailList", detailList);
			result.put("count",count);
		}	
		return result;
	}
		


	@Override
	public void saveMessage(Dto dto) {
		String uid = dto.getAsString("uid");
		Favorite fa = new Favorite();
		String favorite_id = java.util.UUID.randomUUID().toString();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		
		fa.setRecord_id(favorite_id);
		fa.setCustomer_id(customerIdList.get(0).getCustomer_id());
		fa.setInformation_id(dto.getAsString("informationid"));
		fa.setOperater(uid);
		fa.setOperate_date(new Date());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		fa.setDates(sdf.format(new Date()).toString());
		fa.setSource_cd(dto.getAsString("requestsource"));
		fa.setContent_type("info");
		super.getNewDao().insert("message_space.insert_bs_favorite", fa);
	}

	@Override
	public List<Dto> getfavoritelList(String uid) {
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		String customerid = customerIdList.get(0).getCustomer_id();
		List<Favorite> resultList = super.getNewDao().queryForList("message_space.getfavoritelList",customerid);
		List<Dto> result = new ArrayList<Dto>();
		for (int i = 0; resultList!=null && i < resultList.size(); i++) {
			Favorite fav = resultList.get(i);
			Dto resultMap = new BaseDto();
			//保证原先的版本正常运行，不包括后来更新版本收藏的Banner
			if(!"banner".equals(fav.getContent_type())){
				//收藏id
				resultMap.put("record_id", fav.getRecord_id());
				resultMap.put("information_id", fav.getInformation_id());
				//获取资讯
				List<Information> info = super.getNewDao().queryForList("message_space.getMessage_uid", fav.getInformation_id());
				Information im = info.get(0);
				resultMap.put("imageUrl", im.getImage_url());
				resultMap.put("title", im.getTitle());
				resultMap.put("type", im.getType());
				resultMap.put("dates", fav.getDates());
				resultMap.put("summary", im.getSummary());
				result.add(resultMap);
			}
		}
		return result;
	}

	@Override
	public void delfavoritelList(Dto paramDto) {
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		Map<String,String> map = new HashMap<String,String>();
		map.put("customerid", customerid);
		//资讯和Banner的共享informationid
		map.put("informationid", paramDto.getAsString("informationid"));
		super.getNewDao().delete("message_space.delfavoritelone", map);
	}

	@Override//type date
	public List<Dto> getMessageTop(Dto paramDto) {
		List<Dto> results = new ArrayList<Dto>();
		Map<String,String> map = new HashMap<String,String>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		map.put("end_date", sdf.format(new Date()));
		//改造资讯
		if(paramDto.getAsString("app")==null||"".equals(paramDto.getAsString("app"))){
			map.put("app", "doctor");
		}else{
			map.put("app", paramDto.getAsString("app"));
		}
		map.put("type", paramDto.getAsString("type"));//添加了甜蜜人生专栏
		if("最新".equals(paramDto.getAsString("type"))){
			List<Topinfo> list = super.getNewDao().queryForList("message_space.selectNewTopmessage",map);
			for(Topinfo t : list){
				Dto result = new BaseDto();
				Information in = (Information)super.getNewDao().queryForObject("message_space.getMessage_uid", t.getInformation_id());
				result.put("picurl", t.getBig_image());
				result.put("title", in.getTitle());
				result.put("informationid", in.getInformation_id());
				result.put("position",t.getPosition());
				result.put("summary",in.getSummary());
				results.add(result);
			}
		}else {
			
			List<Topinfo> list = super.getNewDao().queryForList("message_space.selectTopmessage", map);
			for(Topinfo t : list){
				Dto result = new BaseDto();
				Information in = (Information)super.getNewDao().queryForObject("message_space.getMessage_uid", t.getInformation_id());
				result.put("picurl", t.getBig_image());
				result.put("title", in.getTitle());
				result.put("informationid", in.getInformation_id());
				result.put("position",t.getPosition());
				result.put("summary",in.getSummary());
				results.add(result);
			}
		}
		return results;
	}

	@Override
	public List<Dto> getBanner(Dto paramDto) {
		List<Dto> results = new ArrayList<Dto>();
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String nowdate = sdf.format(new Date());
		List<Advert> list = super.getNewDao().queryForList("message_space.getBanner", nowdate);
		for(Advert a : list){
			Dto result = new BaseDto();
			result.put("imageUrl", a.getImageUrl());
			result.put("title", a.getTitle());
			result.put("adid", a.getAdid());
			result.put("bannerUrl", "hdhealth/message/bannerdetail.html?");
			result.put("summary", a.getSummary());
			
			String uid = paramDto.getAsString("uid");
			if("".equals(paramDto.getAsString("uid"))||paramDto.getAsString("uid")==null){
				uid="36c0bd06-2d1a-46ce-985b-fea70071dd03";
			}
			List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
			String customerid = customerIdList.get(0).getCustomer_id();
			Map<String,String> map = new HashMap<String,String>();
			map.put("informationid", a.getAdid());
			map.put("customerid", customerid);
			List<Favorite> adfa = super.getNewDao().queryForList("message_space.selectAdFavorite", map);
			if(adfa.size()>0){
				result.put("exist", "1");
			}else{
				result.put("exist", "0");
			}
			results.add(result);
		}
		return results;
	}

	public void updateMessageInfo(Dto paramDto) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", new Date());
		map.put("id", paramDto.getAsString("record_id"));
		//更新
		super.getNewDao().update("message_space.updateMessageInfo", map);
	}

	public boolean check_message_exist(Dto paramDto) {
		String uid = paramDto.getAsString("uid");
		if("".equals(paramDto.getAsString("uid"))||paramDto.getAsString("uid")==null){
			uid="36c0bd06-2d1a-46ce-985b-fea70071dd03";
		}
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		String customerid = customerIdList.get(0).getCustomer_id();
		Map<String,String> map = new HashMap<String,String>();
		map.put("informationid", paramDto.getAsString("informationid"));
		map.put("customerid", customerid);
		List<Favorite> fa = super.getNewDao().queryForList("message_space.selectFavorite", map);
		if(fa.size()>0){
			return true;
		}else{
			return false;
		}
	}

	public List<Dto> getHealthConsultation(Dto paramDto) {
		String type = paramDto.getAsString("type");
		String name = paramDto.getAsString("name");
		String tizhi = paramDto.getAsString("constitution");
		Map<String, String> conMap = new HashMap<String, String>();
		conMap.put("tizhi", tizhi);
		conMap.put("name", name);
		List<Dto> resultlist = null;
		//type 为所传类型（0：运动；1：膳食；2：睡眠）
		if("0".equals(type)){
			resultlist = super.getNewDao().queryForList("message_space.getsport", conMap);
		}else if("1".equals(type)){
			resultlist = super.getNewDao().queryForList("message_space.getmeal", conMap);
		}else if("2".equals(type)){
			resultlist = super.getNewDao().queryForList("message_space.getsleep", conMap);
		}
		return resultlist;
	}

	@Override
	public Advert getBannerById(String adid) {
		super.getNewDao().update("message_space.updateBannerReadcount", adid);
		return (Advert) super.getNewDao().queryForObject("message_space.getBannerById", adid);
	}
	
	@Override
	public List<Dto> getBannerList(Dto paramDto) {
		List<Dto> results = new ArrayList<Dto>();
		List<Advert> list = super.getNewDao().queryForList("message_space.getBannerList");
		for(Advert a : list){
			Dto result = new BaseDto();
			result.put("imageUrl", a.getImageUrl());	
			result.put("title", a.getTitle());
			result.put("adid", a.getAdid());
			result.put("content", a.getContent());
			result.put("startdate", a.getStartdate());
			result.put("enddate", a.getEnddate());
			result.put("position", a.getPosition());
			result.put("auditResult", a.getAuditResult());
			result.put("status", a.getStatus());
			result.put("dates", a.getDates());
			result.put("createtime", a.getOperate_date());
			result.put("operater", a.getOperater());
			results.add(result);
		}
		return results;
	}

	@Override
	public void addOrUpadateBanner(Dto paramDto) {
		List<Advert> advert = super.getNewDao().queryForList("message_space.selectBanner", paramDto.getAsString("adid"));
		if(advert.size()==0){
			String adid = UUID.randomUUID().toString();
			Advert ad = new Advert();
			ad.setAdid(adid);
			ad.setImageUrl(paramDto.getAsString("imageurl"));
			ad.setContent(paramDto.getAsString("content"));
			ad.setDates(paramDto.getAsString("dates"));
			ad.setPosition(paramDto.getAsString("position"));
			ad.setStatus(paramDto.getAsString("status"));
			ad.setTitle(paramDto.getAsString("title"));
			ad.setSummary(paramDto.getAsString("summary"));
			ad.setOperater(paramDto.getAsString("operater"));
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			try {
				ad.setOperate_date(sdf.parse(paramDto.getAsString("createtime")));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			super.getNewDao().insert("message_space.addBanner", ad);
		}else{
			Advert ad = new Advert();
			ad.setAdid(paramDto.getAsString("adid"));
			ad.setImageUrl(paramDto.getAsString("imageurl"));
			ad.setContent(paramDto.getAsString("content"));
			ad.setDates(paramDto.getAsString("dates"));
			ad.setPosition(paramDto.getAsString("position"));
			ad.setStatus(paramDto.getAsString("status"));
			ad.setTitle(paramDto.getAsString("title"));
			ad.setSummary(paramDto.getAsString("summary"));
			ad.setOperater(paramDto.getAsString("operater"));
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			try {
				ad.setOperate_date(sdf.parse(paramDto.getAsString("createtime")));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			super.getNewDao().update("message_space.updateBanner", ad);
		}
	}

	@Override
	public void deleteBanner(Dto paramDto) {
		super.getNewDao().update("message_space.deleteBanner", paramDto.getAsString("adid"));
	}

	@Override
	public void saveBanner(Dto dto) {

		String uid = dto.getAsString("uid");
		Favorite fa = new Favorite();
		String adfavorite_id = java.util.UUID.randomUUID().toString();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		
		fa.setRecord_id(adfavorite_id);
		fa.setCustomer_id(customerIdList.get(0).getCustomer_id());
		//保存BannerId到favorite中
		fa.setInformation_id(dto.getAsString("adid"));
		fa.setOperater(uid);
		fa.setOperate_date(new Date());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		fa.setDates(sdf.format(new Date()).toString());
		fa.setSource_cd(dto.getAsString("requestsource"));
		//保存Banner标识
		fa.setContent_type("banner");
		super.getNewDao().insert("message_space.insert_bs_adfavorite", fa);
	}

	@Override
	public void deladfavoriteList(Dto paramDto) {
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		Map<String,String> map = new HashMap<String,String>();
		map.put("customerid", customerid);
		map.put("adid", paramDto.getAsString("adid"));
		map.put("content_type", "banner");
		super.getNewDao().delete("message_space.deladfavoritelone", map);
	}

	@Override
	public List<Dto> getfavoriteInfoAndBannerList(String uid) {
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		String customerid = customerIdList.get(0).getCustomer_id();
		List<Favorite> resultList = super.getNewDao().queryForList("message_space.getfavoritelList",customerid);
		List<Dto> result = new ArrayList<Dto>();
		for (int i = 0; resultList!=null && i < resultList.size(); i++) {
			Favorite fav = resultList.get(i);
			Dto resultMap = new BaseDto();
			//判断资讯OR首页Banner
			//资讯 null的情况之前的版本存储CONTENT_TYPE为空
			if(!"banner".equals(fav.getContent_type())){
				//收藏id
				resultMap.put("record_id", fav.getRecord_id());
				resultMap.put("information_id", fav.getInformation_id());
				//获取资讯
				List<Information> info = super.getNewDao().queryForList("message_space.getMessage_uid", fav.getInformation_id());
				Information im = info.get(0);
				resultMap.put("imageUrl", im.getImage_url());
				resultMap.put("title", im.getTitle());
				resultMap.put("type", im.getType());
				resultMap.put("dates", fav.getDates());
				resultMap.put("summary", im.getSummary());
				//null 或  "" 或  "info" 
				String content_type=fav.getContent_type();
				if(content_type==null||"".equals(content_type)){
					content_type="info";
				}
				resultMap.put("content_type",content_type);
			//首页Banner
			}else if("banner".equals(fav.getContent_type())){
				//收藏id
				resultMap.put("record_id", fav.getRecord_id());
				resultMap.put("information_id", fav.getInformation_id());
				//获取资讯
				List<Advert> info = super.getNewDao().queryForList("message_space.getBannerById", fav.getInformation_id());
				Advert ad = info.get(0);
				resultMap.put("imageUrl", ad.getImageUrl());
				resultMap.put("title", ad.getTitle());
				resultMap.put("type", "");
				resultMap.put("dates", fav.getDates());
				resultMap.put("summary", ad.getSummary());
				//"banner"
				resultMap.put("content_type",fav.getContent_type());
				
			}
			result.add(resultMap);
		}
		return result;
	}

	@Override
	public boolean check_banner_exist(Dto paramDto) {
		String uid = paramDto.getAsString("uid");
		if("".equals(paramDto.getAsString("uid"))||paramDto.getAsString("uid")==null){
			uid="36c0bd06-2d1a-46ce-985b-fea70071dd03";
		}
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", uid);
		String customerid = customerIdList.get(0).getCustomer_id();
		Map<String,String> map = new HashMap<String,String>();
		map.put("informationid", paramDto.getAsString("adid"));
		map.put("customerid", customerid);
		List<Favorite> fa = super.getNewDao().queryForList("message_space.selectAdFavorite", map);
		if(fa.size()>0){
			return true;
		}else{
			return false;
		}
	}
}
